/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package btracker;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;

/**
 * @version
 * @author Sammy
 */
public class Calendar extends JPanel {
    //GUI components

    static JPanel pnlTableCalendar;
    static JTable tblCalendar;
    static JLabel lblYear;
    static JLabel lblMonth;
    static JPanel pnlDate;
    static JButton btnLeft;
    static JButton btnRight;
    //////////////
    //Date Variables & Constants////
    static int Year = 2010;
    static int Counter = 0;
    public static final String[] MONTH_DATE = {"             January", "           February", "             March", "             April", "             May", "             June", "             July", "             August", "         September", "            October", "          November", "          December"};
    static Object[] WEEK_DATE = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
    static Object[][] DAY_DATE = {{"1", "2", "3", "4", "5", "6", "7"}, {"8", "9", "10", "11", "12", "13", "14"}, {"15", "16", "18", "19", "20", "21", "22"},
        {"23", "24", "25", "26", "27", "28", "29"}, {"30", "31", "", "", "", "", ""}};
    static CalendarModel model = new CalendarModel();
    /////////////////////////////

    public static void drawCalendar() {




        /// Declare commponents///
        MainWindow.pnlCalendar = new JPanel();
        pnlTableCalendar = new JPanel();



        //--------------------pnlCalendar////////////////////

        //button Handler
        ButtonHandler onClick = new ButtonHandler();

        //Set up left button
        btnLeft = new JButton(">");
        btnLeft.addActionListener(onClick);
        btnLeft.setPreferredSize(new Dimension(50, btnLeft.getHeight()));
        btnLeft.setActionCommand("right");

        //Set up Right Button
        btnRight = new JButton("<");
        btnRight.addActionListener(onClick);
        btnRight.setPreferredSize(new Dimension(50, btnRight.getHeight()));
        btnRight.setActionCommand("left");

        /// Displays the year
        lblYear = new JLabel(" " + Integer.toString(Year));
        lblYear.setForeground(Color.red);
        lblYear.setFont(new Font(" " + Integer.toString(Year), Font.PLAIN, 23));

        /// Displays the Month

        lblMonth = new JLabel(MONTH_DATE[Counter]);
        lblMonth.setForeground(Color.red);
        lblMonth.setFont(new Font(MONTH_DATE[Counter], Font.PLAIN, 24));

        /// Displays the date and the buttons

        pnlDate = new JPanel(new GridLayout(0, 4));


        // add components to the Date panel
        pnlDate.add(btnRight);
        pnlDate.add(lblMonth);
        pnlDate.add(lblYear);
        pnlDate.add(btnLeft);

        pnlDate.repaint();

        MainWindow.pnlCalendar.setLayout(new BorderLayout());

        ///Add to main frame
        MainWindow.pnlCalendar.add(pnlDate, BorderLayout.PAGE_START);

        //***************************************************************


        //--------------------pnlTableCalendar////////////////////

        

        JTable tblCalendar1 = new JTable(model);

        //Config Table//
        tblCalendar1.setRowHeight(85);
        tblCalendar1.getTableHeader().setReorderingAllowed(false);
        tblCalendar1.setPreferredSize(new Dimension(500, 770));
        tblCalendar1.setCellSelectionEnabled(false);
        tblCalendar1.setCellSelectionEnabled(false);
        model.setMonth(Year, Counter);
        ////////////////

        // Config Panel//
        pnlTableCalendar.setLayout(new BorderLayout());
        pnlTableCalendar.add(tblCalendar1, BorderLayout.CENTER);
        pnlTableCalendar.add(tblCalendar1.getTableHeader(), BorderLayout.PAGE_START);
        //////////////

        //ADD TO MAINWINDOW
        MainWindow.pnlCalendar.add(pnlTableCalendar, BorderLayout.CENTER);


    } //DrawCalender

    private static class ButtonHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            pnlTableCalendar.repaint();
            String command = e.getActionCommand();

            ///// RIGHT BUTTON ACTION
            if (command.equalsIgnoreCase("right")) {




                if (Counter >= 11) { //LINK TO NEXT YEAR
                    //When You get to December Go Forward to January
                    //and Add a year
                    Counter = 0;
                    Year++;
                    model.setMonth(Year, Counter);
                    lblYear.setText(Integer.toString(Year));
                    lblMonth.setText(MONTH_DATE[Counter]);
                    model.setMonth(Year, Counter);

                } else {//NEXT MONTH

                    Counter++;
                    lblMonth.setText(MONTH_DATE[Counter]);
                    model.setMonth(Year, Counter);


                }





                ///// LEFT BUTTON ACTION
            } else if (command.equalsIgnoreCase("left")) {

                if (Counter <= 0) { // LINK TO LAST MONTH
                    //When You get to January Go backwards to December
                    // Remove a year
                    Counter = 11;
                    Year--;
                    lblYear.setText(Integer.toString(Year));
                    lblMonth.setText(MONTH_DATE[Counter]);
                    model.setMonth(Year, Counter);

                } else {// BACK ONE MONTH

                    Counter--;
                    model.setMonth(Year, Counter);
                    lblMonth.setText(MONTH_DATE[Counter]);
                }



            }

        }
    }//Button Handler

 
}//Calendar class

class CalendarModel extends AbstractTableModel {

    public static final String[] MONTH_DATE = {"             January", "           February", "             March", "             April", "             May", "             June", "             July", "             August", "         September", "            October", "          November", "          December"};
    static String[] WEEK_DATE = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
   
 int[] numDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    

  String[][] calendar = new String[7][7];

  public CalendarModel() {
     for (int i = 0; i < 7; ++i)
      for (int j = 0; j < 7; ++j)
        calendar[i][j] = " ";
  }

   public int getColumnCount() {
            return WEEK_DATE.length;
    }

  public int getRowCount() {
    return 8;
  }

  public String getColumnName(int col) {
            return WEEK_DATE[col];
        }

 public boolean isCellEditable(int row, int col) {
   return false;
 }

  public Object getValueAt(int row, int column) {
    return calendar[row][column];
  }

  public void setValueAt(Object value, int row, int column) {
    calendar[row][column] = (String) value;
  }

  public void setMonth(int year, int month) {
    for (int i = 0; i < 7; ++i)
      for (int j = 0; j < 7; ++j)
        calendar[i][j] = " ";
    java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
    cal.set(year, month, 1);
    int offset = cal.get(java.util.GregorianCalendar.DAY_OF_WEEK) - 1;
    offset += 7;
    int num = daysInMonth(year, month);
    for (int i = 0; i < num; ++i) {
      calendar[(offset / 7)-1][(offset % 7)] = Integer.toString(i + 1);
      ++offset;
    }
  }

  public boolean isLeapYear(int year) {
    if (year % 4 == 0)
      return true;
    return false;
  }

  public int daysInMonth(int year, int month) {
    
      int days = numDays[month];

    if (month == 1 && isLeapYear(year))
      ++days;
    return days;
  }
}



